﻿' Copyright (c) 2013 DotNetNuke Corporation
'  All rights reserved.
' 
' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
' TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
' THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
' CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
' DEALINGS IN THE SOFTWARE.
' 
Imports System
Imports System.Data
Imports System.Data.SqlClient
Imports Microsoft.ApplicationBlocks.Data

Imports DotNetNuke.Common.Utilities
Imports DotNetNuke.Framework.Providers

Namespace Data

    ''' -----------------------------------------------------------------------------
    ''' <summary>
    ''' SQL Server implementation of the abstract DataProvider class
    ''' 
    ''' This concreted data provider class provides the implementation of the abstract methods 
    ''' from data dataprovider.cs
    ''' 
    ''' In most cases you will only modify the Public methods region below.
    ''' </summary>
    ''' <remarks>
    ''' </remarks>
    ''' <history>
    ''' </history>
    ''' -----------------------------------------------------------------------------
    Public Class SqlDataProvider

        Inherits DataProvider

#Region "Private Members"

        Private Const ProviderType As String = "data"
        Private Const ModuleQualifier As String = "AccordionTabs_"

        Private _providerConfiguration As ProviderConfiguration = ProviderConfiguration.GetProviderConfiguration(ProviderType)
        Private _connectionString As String
        Private _providerPath As String
        Private _objectQualifier As String
        Private _databaseOwner As String

#End Region

#Region "Constructors"

        Public Sub New()

            ' Read the configuration specific information for this provider
            Dim objProvider As Provider = CType(_providerConfiguration.Providers(_providerConfiguration.DefaultProvider), Provider)

            ' Read the attributes for this provider

            'Get Connection string from web.config
            _connectionString = Config.GetConnectionString()

            If _connectionString = "" Then
                ' Use connection string specified in provider
                _connectionString = objProvider.Attributes("connectionString")
            End If

            _providerPath = objProvider.Attributes("providerPath")

            _objectQualifier = objProvider.Attributes("objectQualifier")
            If _objectQualifier <> "" And _objectQualifier.EndsWith("_") = False Then
                _objectQualifier += "_"
            End If

            _databaseOwner = objProvider.Attributes("databaseOwner")
            If _databaseOwner <> "" And _databaseOwner.EndsWith(".") = False Then
                _databaseOwner += "."
            End If

        End Sub

#End Region

#Region "Properties"

        Public ReadOnly Property ConnectionString() As String
            Get
                Return _connectionString
            End Get
        End Property

        Public ReadOnly Property ProviderPath() As String
            Get
                Return _providerPath
            End Get
        End Property

        Public ReadOnly Property ObjectQualifier() As String
            Get
                Return _objectQualifier
            End Get
        End Property

        Public ReadOnly Property DatabaseOwner() As String
            Get
                Return _databaseOwner
            End Get
        End Property

        Private ReadOnly Property NamePrefix() As String
            Get
                Return DatabaseOwner + ObjectQualifier + ModuleQualifier
            End Get
        End Property

#End Region

#Region "Private Methods"

        Private Function GetNull(ByVal Field As Object) As Object
            Return DotNetNuke.Common.Utilities.Null.GetNull(Field, DBNull.Value)
        End Function

#End Region

#Region "Public Methods"

        Public Overrides Function GetTabs(ByVal ModuleID As Integer) As IDataReader
            Try
                Return CType(SqlHelper.ExecuteReader(ConnectionString, NamePrefix + "GetTabs", ModuleID), IDataReader)
            Catch exc As Exception
                '
                'TODO: Error Handling
                '
                Return Nothing
            End Try
        End Function

        Public Overrides Function GetTabsReverse(ModuleID As Integer) As IDataReader
            Try
                Return CType(SqlHelper.ExecuteReader(ConnectionString, NamePrefix + "GetTabsReverse", ModuleID), IDataReader)
            Catch ex As Exception
                '
                'TODO: Error Handling
                '
                Return Nothing
            End Try
        End Function

        Public Overrides Function GetSingleTab(ByVal ModuleID As Integer, ByVal ItemID As Integer) As IDataReader
            Try
                Return CType(SqlHelper.ExecuteReader(ConnectionString, NamePrefix + "GetSingleTab", ModuleID, ItemID), IDataReader)
            Catch ex As Exception
                '
                'TODO: Error Handling
                '
                Return Nothing
            End Try
        End Function



        Public Overrides Sub ModifyTab(ByVal ModuleID As Integer, ByVal ItemID As Integer, ByVal TitleOfTab As String, _
                                                 ByVal ContentsOfTab As String, ByVal IsVisible As Boolean, ByVal SortOrder As Integer)
            Try
                SqlHelper.ExecuteNonQuery(ConnectionString, NamePrefix + "ModifyTab", ModuleID, ItemID, TitleOfTab, ContentsOfTab, IsVisible, SortOrder)
            Catch ex As Exception
                '
                'TODO: Error Handling
                '
            End Try
        End Sub

        Public Overrides Sub ModifyTabSortOrder(ModuleID As Integer, ItemID As Integer, SortOrder As Integer)
            Try
                SqlHelper.ExecuteNonQuery(ConnectionString, NamePrefix + "ModifyTabSortOrder", ModuleID, ItemID, SortOrder)
            Catch ex As Exception
                '
                'TODO: Error Handling
                '
            End Try
        End Sub

        Public Overrides Sub InsertTab(ByVal ModuleID As Integer, ByVal TitleOfTab As String, ByVal ContentsOfTab As String, _
                                                 ByVal IsVisible As Boolean, ByVal SortOrder As Integer)
            Try
                SqlHelper.ExecuteNonQuery(ConnectionString, NamePrefix + "InsertTab", ModuleID, TitleOfTab, ContentsOfTab, IsVisible, SortOrder)
            Catch ex As Exception
                '
                'TODO: Error Handling
                '
            End Try
        End Sub

        Public Overrides Sub DeleteTab(ByVal ModuleID As Integer, ByVal ItemID As Integer)
            Try
                SqlHelper.ExecuteNonQuery(ConnectionString, NamePrefix + "DeleteTab", ModuleID, ItemID)
            Catch ex As Exception
                '
                'TODO: Error Handling
                '
            End Try
        End Sub
        Public Overrides Sub DeleteAllTabs(ByVal ModuleID As Integer)
            Try
                SqlHelper.ExecuteNonQuery(ConnectionString, NamePrefix + "DeleteAllTabs", ModuleID)
            Catch ex As Exception
                '
                'TODO: Error Handling
                '
            End Try
        End Sub

        Public Overrides Function GetDisplayStyles(ByVal ModuleID As Integer) As IDataReader
            Try
                Return CType(SqlHelper.ExecuteReader(ConnectionString, NamePrefix + "GetDisplayStyles", ModuleID), IDataReader)
            Catch ex As Exception
                '
                'TODO: Error Handling
                '
                Return Nothing
            End Try
        End Function
        Public Overrides Function GetDisplayStyle(ByVal ModuleID As Integer, ByVal DisplayStyleID As Integer) As IDataReader
            Try
                Return CType(SqlHelper.ExecuteReader(ConnectionString, NamePrefix + "GetDisplayStyle", ModuleID, DisplayStyleID), IDataReader)
            Catch ex As Exception
                '
                'TODO: Error Handling
                '
                Return Nothing
            End Try
        End Function

        Public Overrides Sub AddDisplayStyle(ByVal ModuleID As Integer, ByVal DisplayStyle As String, _
                                                ByVal DisplayStyleNotes As String)
            Try
                SqlHelper.ExecuteNonQuery(ConnectionString, NamePrefix + "InsertDisplayStyle", ModuleID, DisplayStyle, DisplayStyleNotes)
            Catch ex As Exception
                '
                'TODO: Error Handling
                '
            End Try
        End Sub
        Public Overrides Sub ModifyDisplayStyle(ByVal ModuleID As Integer, ByVal DisplayStyleID As String, _
                                                   ByVal DisplayStyle As Integer, ByVal DisplayStyleNotes As String)
            Try
                SqlHelper.ExecuteNonQuery(ConnectionString, NamePrefix + "UpdateDisplayStyle", ModuleID, DisplayStyleID, DisplayStyle, DisplayStyleNotes)
            Catch ex As Exception
                '
                'TODO: Error Handling
                '
            End Try
        End Sub
        Public Overrides Sub DeleteDisplayStyle(ByVal ModuleID As Integer, ByVal DisplayStyleID As Integer)
            Try
                SqlHelper.ExecuteNonQuery(ConnectionString, NamePrefix + "DeleteDisplayStyle", ModuleID, DisplayStyleID)
            Catch ex As Exception
                '
                'TODO: Error Handling
                '
            End Try
        End Sub
        Public Overrides Sub DeleteAllDisplayStyles(ByVal ModuleID As Integer)
            Try
                SqlHelper.ExecuteNonQuery(ConnectionString, NamePrefix + "DeleteAllDisplayStyles", ModuleID)
            Catch ex As Exception
                '
                'TODO: Error Handling
                '
            End Try
        End Sub
#End Region

    End Class

End Namespace